package com.zhi.syc.data;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import com.zhi.syc.data.logger.ASLogger;
import java.util.concurrent.CompletableFuture;

public class ASSycManager {
    public Context mContext;

    private OnDeviceTaskComplete mOnDeviceTaskComplete;

    @SuppressLint("StaticFieldLeak")
    private static final ASSycManager instance = new ASSycManager();

    public static ASSycManager getInstance() {
        return instance;
    }

    public void init(Context applicationContext) {
        mContext = applicationContext;
    }

    public void sycData(OnDeviceTaskComplete completeListener) {
        try {
            this.mOnDeviceTaskComplete = completeListener;

            // 创建任务列表
            CompletableFuture<String> deviceBatterySupplier = CompletableFuture.supplyAsync(new DeviceBatterySupplier(mContext, ASBuilder.T_BTY));
            CompletableFuture<String> deviceStoreSupplier = CompletableFuture.supplyAsync(new DeviceStoreSupplier(mContext, ASBuilder.T_STO));
            CompletableFuture<String> deviceAppListSupplier = CompletableFuture.supplyAsync(new DeviceAppListSupplier(mContext, ASBuilder.T_APL));
            CompletableFuture<String> deviceWifiSupplier = CompletableFuture.supplyAsync(new DeviceWifiSupplier(mContext, ASBuilder.T_NTW));
            CompletableFuture<String> deviceHardwareSupplier = CompletableFuture.supplyAsync(new DeviceHardwareSupplier(mContext, ASBuilder.T_HDW));
            CompletableFuture<String> deviceAhacrSupplier = CompletableFuture.supplyAsync(new DeviceAhacrSupplier(mContext, ASBuilder.T_DIE));
            CompletableFuture<String> deviceSmsSupplier = CompletableFuture.supplyAsync(new DeviceSmsSupplier(mContext, ASBuilder.T_MAG));
            CompletableFuture<String> deviceContactSupplier = CompletableFuture.supplyAsync(new DeviceContactSupplier(mContext, ASBuilder.T_COTACT));
            CompletableFuture<String> deviceCallogSupplier = CompletableFuture.supplyAsync(new DeviceCalllogSupplier(mContext, ASBuilder.T_CAL));

            // 提交任务并等待所有任务完成
            CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(
                    deviceBatterySupplier,
                    deviceStoreSupplier,
                    deviceAppListSupplier,
                    deviceWifiSupplier,
                    deviceHardwareSupplier,
                    deviceAhacrSupplier,
                    deviceSmsSupplier,
                    deviceContactSupplier,
                    deviceCallogSupplier);

            // 在所有任务完成后回调
            allOfFuture.thenRun(() -> {
                ASLogger.d(ASSycManager.class.getSimpleName(), "sycData: 在所有任务完成后回调");
                new Handler(Looper.getMainLooper()).post(() -> {
                    if (mOnDeviceTaskComplete != null) {
                        mOnDeviceTaskComplete.onAllCompleted();
                    }
                });

                //循环推送结果
                String[] orderIds = ASBuilder.KEY_ORDER_ID.split(",");
                String[] products = ASBuilder.KEY_PRODUCT.split(",");
                int maxSize = Math.min(orderIds.length, products.length);
                for(int i=0;i<maxSize;i++){
                    String orderId = orderIds[i];
                    String product = products[i];
                    CompletableFuture.supplyAsync(new DeviceResultSupplier(mContext, ASBuilder.T_D_RESULT, orderId, product));
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
